home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Tutorial / Script5b.vu < prev    next >
Encoding:
Text File  |  1998-06-04  |  7.3 KB  |  253 lines  |  [TEXT/MPS ]

  1. # **********************************************************************
  2. # File:             Script 5 - TargetB.vu
  3. #
  4. # Purpose:            To demonstrate the ability to use message passing
  5. #
  6. # Prerequistes:        This script assumes that DrawShapesVU is the
  7. #                    currently active application on the target 
  8. #                    and that you have turned off Key Repeat
  9. #                    in the control panel of the target computer.
  10. #
  11. # Written by:        David Gaxiola
  12. #
  13. # Copyright © 1992 by Apple Computer, Inc., all rights reserved.
  14. #
  15. # **********************************************************************
  16.  
  17. Libraries "StandardDialogs.vulib", "Time.vulib";
  18.  
  19. # **********************************************************************
  20. # Calculate the absolute difference between two numbers.
  21. task Diff( Num1, Num2 ) 
  22. begin
  23.     theDifference := Num1 - Num2;
  24.     if (theDifference < 0) 
  25.     begin
  26.         theDifference := -theDifference;
  27.     end;
  28.     return theDifference;
  29. end;
  30.  
  31. # **********************************************************************
  32. # Wait for the the partner to give the "go."
  33. # Using the wait() task lessens the burden on Virtual User.
  34. task HoldForOther( )
  35. begin
  36.     global gPartner;
  37.     
  38.     status := {''};
  39.     println "Holding for {gPartner} at ", GetCurrentTime( false ), "…"; 
  40.     while not (status = 'go') 
  41.     begin
  42.         status := Receive([actor t:gPartner]);
  43.         if not ( status = 'go' ) 
  44.             wait( 2 );
  45.     end;
  46.     print "Proceeding at ", GetCurrentTime( false ), "…";
  47. end;
  48.  
  49. # **********************************************************************
  50. # Select a given or random tool from the side palette.
  51. task SelectTool(toolNum := 1, randomDraw := false) 
  52. begin
  53.     global gWindowDim;
  54.     global gToolColumn;
  55.     global gToolRow;
  56.     
  57.     if ( randomDraw ) 
  58.         toolNum := Random(2,4);
  59.     move a:{ (gWindowDim[1] + gToolColumn), 
  60.                 (gWindowDim[2] + gToolRow[toolNum]) };
  61.     click;
  62. end;
  63.  
  64. # **********************************************************************
  65. # Select a random color from the Color Picker dialog box.
  66. task SelectFromColorPicker( ) 
  67. begin
  68.     global gPickerMaxScrollValue;
  69.     global gColorPickerFixedCenter;
  70.     global gColorPickerRadius;
  71.  
  72.     match [ window o:1 r:?colorPickerDim ];
  73.     scrollValue := Random( 1, gPickerMaxScrollValue );
  74.     scroll[ scrollbar w:[ window o:1 ]] a:{ scrollValue, 
  75.     gPickerMaxScrollValue };
  76.     wait( 1 );
  77.     colorPickerCenter := { (gColorPickerFixedCenter[1] +
  78.                             colorPickerDim[1]), 
  79.                             (gColorPickerFixedCenter[2] + 
  80.                             colorPickerDim[2]) };
  81.     xPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  82.     yPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  83.  
  84.     while ( (xPos * xPos) + (yPos * yPos) > gColorPickerRadius *
  85.             gColorPickerRadius ) 
  86.     begin
  87.         xPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  88.         yPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  89.     end;
  90.  
  91.     x := gColorPickerCenter[1] + xPos;
  92.     y := gColorPickerCenter[2] + yPos;
  93.     move a:{ x, y };                    
  94.     click;    
  95.     select [ button t:'OK' w:[ window o:1 ]]!;        
  96. end;
  97.  
  98. # **********************************************************************
  99. # Task to select one of the seven preset colors or the Color Picker
  100. task SelectColor(colorNum := 1, randomColor := false) 
  101. begin
  102.     global gColorAvail;
  103.  
  104.     if (gColorAvail)  
  105.     begin
  106.         if randomColor  
  107.         begin
  108.             colorNum := Random( 1, 9 );
  109.             while ( colorNum = 8 )  
  110.                 colorNum := Random( 1, 9 );
  111.         end;
  112.         select[ menuItem o:colorNum m:[ menu t:'Colors' ]];
  113.         if colorNum = 9  
  114.             SelectFromColorPicker( );
  115.     end;
  116. end;
  117.  
  118. # *********************************************************************
  119. # Task to select one of the five possible shades
  120. task SelectShade(shadeNum := 1, randomShade := false) 
  121. begin
  122.     global gShadeMenuPos;
  123.     global gShadeBlockSize;
  124.     global gShadePositions;
  125.  
  126.     shadeLoc := gShadePositions[shadeNum];
  127.     if ( randomShade )  
  128.     begin
  129.         shadeNum := Random( 1, 5 );
  130.         ShadeLoc := gShadePositions[shadeNum];
  131.     end;
  132.     x := gShadeMenuPos[1];
  133.     y := gShadeMenuPos[2];
  134.     move a:{ x, y };
  135.     pressMouse;    
  136.     wait( 1 );
  137.     x := gShadeBlockSize * shadeLoc[1];
  138.     y := gShadeBlockSize * shadeLoc[2];
  139.     move r:{ x, y };
  140.     Wait( 1 );    # for slower targets
  141.     releaseMouse;
  142. end;
  143.  
  144. # **********************************************************************
  145. # Task to draw a rectangular shape
  146. task DrawRandomShapeWithTool( ) 
  147. begin
  148.     global gWindowDim;
  149.     global gWindowMod;
  150.  
  151.     range1 := ( gWindowDim[1] + gWindowMod[1] );
  152.     range2 := ( gWindowDim[2] + gWindowMod[2] );
  153.     range3 := ( gWindowDim[3] + gWindowMod[3] );
  154.     range4 := ( gWindowDim[4] + gWindowMod[4] );
  155.     xStart := Random( range1, range3 );
  156.     yStart := Random( range2, range4 );
  157.     xStop := Random( range1, range3 );
  158.     yStop := Random( range2, range4 );
  159.  
  160.     while ( Diff( xStart, xStop ) < 40 )  
  161.     begin
  162.         xStart := Random( range1, range3 );
  163.         xStop := Random( range1, range3 );
  164.     end;
  165.     
  166.     while ( Diff( yStart, yStop ) < 40 )  
  167.     begin
  168.         yStart := Random( range2, range4 );
  169.         yStop := Random( range2, range4 );    
  170.     end;
  171.     
  172.     move a:{ xStart,yStart }; 
  173.     pressMouse;
  174.     wait( 1 ); # for slower targer
  175.     move a:{ xStop,yStop };  
  176.     releaseMouse;
  177. end;
  178.  
  179. # Begin Main Body *****************************************************
  180. # Note:  Replace "Virtual User-1" with the name of the second target.
  181. script Tutorial5BMain(gPartner := "Virtual User-1", maxIterations := 2, saveAsFilename := 'Tutorial 5B Example.ds')
  182. begin
  183. # Global variable definitions 
  184.     global gToolColumn := 18;
  185.     global gToolRow := { 40, 80,120,160 };
  186.     global gWindowMod := { 41, 21, -21, -21 };
  187.     global gPickerMaxScrollValue := 32767;
  188.     global gColorPickerFixedCenter := {293,137};
  189.     global gColorPickerRadius := 100;
  190.     global gShadeMenuPos    := {125,10};
  191.     global gShadeBlockSize := 25;
  192.     global gShadePositions := { {0,1},{1,1},{2,1},{0,2},{1,2},{2,2} };
  193.     global gWindowDim := { };
  194.     global gScreenDim := { };
  195.     global gColorAvail := false;
  196.     global gStatus := '';
  197.     global gPartner;
  198.  
  199. # Set the actor's behavior.
  200.     MouseSpeed( 18 );
  201.     Patience( 4 );
  202.  
  203. # Open up communications with the other target
  204.     println "Attempting to 'open' communication with target {gPartner}.";
  205.     while not (gStatus = 'open') 
  206.     begin
  207.         gStatus := OpenSession([actor t:gPartner]);
  208.     end;
  209.  
  210. # Get information about the target's environment.
  211.     match [ screen r:?gScreenDim m:true ];
  212.     match [ menu t:?theFifthMenu o:5 ];
  213.  
  214.     if ( theFifthMenu = 'Colors' ) 
  215.         gColorAvail := true;
  216.     else 
  217.         gColorAvail := false;
  218.  
  219. # Create a new file and set up the window to the appropriate size.
  220.     println "Creating and setting up new DrawShapes document.";
  221.     select [ menuItem t:'New' m:[ menu t:'File' ]];
  222.     drag [ window t:/Untitled-≈/ o:1 ] a:{ 1, 21 };
  223.     size [ window t:/Untitled-≈/ o:1 ] w:(gScreenDim[3] - 2) h:(gScreenDim[4] - 22);
  224.     match [ window r:?gWindowDim o:1 ];
  225.     if ( Diff( (gWindowDim[2] + gWindowMod[2]), 
  226.             (gWindowDim[4] + gWindowMod[4]) ) > 645 ) 
  227.     begin
  228.         temp := WindowDim[2] + gWindowMod[2] + 645 - gWindowDim[4];
  229.         gWindowMod := Replace(temp,4,gWindowMod);
  230.     end;
  231.  
  232. # Drawshapes and communicate with the other target.
  233.     currentIteration := 1;
  234.     while ( currentIteration <= maxIterations )  
  235.     begin
  236.         HoldForOther();
  237.         SelectTool( 1, true );
  238.         DrawRandomShapeWithTool( );
  239.         SelectShade( 1, true );
  240.         SelectColor( 1, true );
  241.         currentIteration := currentIteration + 1;
  242.         println "In iteration number {currentIteration}.";
  243.         Send([actor t:gPartner],{'go'});
  244.         println "Go {gPartner}!";
  245.     end;
  246.  
  247. # Save the file and end the communications session.
  248.     println "Saving document and closing session.";
  249.     DoSaveAs( saveAsFilename );
  250.     CloseSession([actor t:gPartner]);
  251.     println "Finished!";
  252. end; # End Main Body *****************************************************
  253.